#include <QDebug>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QProgressBar>
#include <QLabel>
#include <QStatusBar>
#include <QTextCodec>
#include <QSignalMapper>
#include <QEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QStyleFactory>
#include "../../settings.h"
#include <QSettings>
#include "creditcarddelegate.h"

#define GetListAtRowRole        Qt::UserRole+1
#define GetFileNameRole         Qt::UserRole+2
#define SaveToFileRole          Qt::UserRole+3
#define LoadFromRole            Qt::UserRole+4
#define GetListSizeRole         Qt::UserRole+5
#define GetListProviders        Qt::UserRole+6
#define SetFilenameRole         Qt::UserRole+7
#define SetProgressBarRole      Qt::UserRole+8
#define GetProgressBarRole      Qt::UserRole+9
#define GetStatusStringRole     Qt::UserRole+10
#define GetStatusTypeRole       Qt::UserRole+11
#define SetStatusStringRole     Qt::UserRole+12
#define SetStatusTypeRole       Qt::UserRole+13
#define GetLoginTypesRole       Qt::UserRole+14

CreditCardDelegate::CreditCardDelegate()
{

}

CreditCardDelegate::~CreditCardDelegate()
{

}

QWidget *CreditCardDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
        if(index.column() == 0)  { // name
            QLineEdit *editor = new QLineEdit(parent);
            QString currentText = index.model()->data(index, Qt::DisplayRole).toString();
            editor->setText(currentText);
            return editor;
        }
        else if (index.column() == 1) {//credit card
            QLineEdit *editor = new QLineEdit(parent);
            //editor->setAlignment(Qt::AlignCenter);
            QString currentText = index.model()->data(index, Qt::DisplayRole).toString();
            editor->setInputMask("9999 9999 9999 9999");
            editor->setText(currentText);
            editor->setCursorPosition(0);
            return editor;
        }else if(index.column() == 2){//exp month
            QSpinBox *expMonth= new QSpinBox(parent);
            expMonth->setRange(1,12);
            expMonth->setValue(index.model()->data(index, Qt::DisplayRole).toInt());
            return expMonth;
        }else if (index.column() == 3) { //exp year
            QSpinBox *expYear= new QSpinBox(parent);
            expYear->setRange(2015,2037);
            expYear->setValue(index.model()->data(index, Qt::DisplayRole).toInt());
            return expYear;
        }else if(index.column() == 4)  { // cvv
            QLineEdit *editor = new QLineEdit(parent);
            QString currentText = index.model()->data(index, Qt::DisplayRole).toString();
            editor->setText(currentText);
            return editor;
        }else if(index.column() == 5)  { // Save
            QPushButton *buttonSave = new QPushButton("Save", parent);
            return buttonSave;
        }else if(index.column() == 6)  { // Load
            QPushButton *buttonLoad = new QPushButton("Load", parent);
            return buttonLoad;
        }
}

void CreditCardDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
        if (index.column() == 0) {//name
            QLineEdit *name = qobject_cast<QLineEdit *>(editor);
            if (name) {
                name->setText(index.model()->data(index, Qt::EditRole).toString());
            }
        }
        else if (index.column() == 1) {//card number
            QLineEdit *numberEditor = qobject_cast<QLineEdit *>(editor);
            if (numberEditor) {
                //nameEditor->setAlignment(Qt::AlignCenter);
                numberEditor->setInputMask("9999 9999 9999 9999");
                numberEditor->setText(index.model()->data(index, Qt::EditRole).toString());
                numberEditor->setCursorPosition(0);
            }
        }else if(index.column() == 2){//exp month
            QSpinBox *expMonth = static_cast<QSpinBox *>(editor);
            if (expMonth) {
                expMonth->setRange(1,12);
                expMonth->setValue(index.model()->data(index, Qt::DisplayRole).toInt());
            }
        }else if (index.column() == 3) {//exp year
            QSpinBox *expYear = static_cast<QSpinBox *>(editor);
            if (expYear) {
                expYear->setRange(2015,2037);
                expYear->setValue(index.model()->data(index, Qt::DisplayRole).toInt());
            }
        }else if(index.column() == 4)  { // cvv
            QLineEdit *cvv = qobject_cast<QLineEdit *>(editor);
            if (cvv) {
                cvv->setText(index.model()->data(index, Qt::EditRole).toString());
            }
        }else if(index.column() == 5)  { // Save
            QPushButton *buttonSave = static_cast <QPushButton *>(editor);
            buttonSave->setText("Save");

            buttonSave->setProperty("index", index);
            connect(buttonSave, SIGNAL(clicked()), this, SLOT(SaveItem()));
        }else if(index.column() == 6)  { // Load
            QPushButton *buttonLoad = static_cast <QPushButton *>(editor);
            buttonLoad->setText("Load");

            buttonLoad->setProperty("index", index);
            connect(buttonLoad, SIGNAL(clicked()), this, SLOT(LoadItemFromFile()));
        }
}

void CreditCardDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
        if (index.column() == 0) {
            QLineEdit *name = qobject_cast<QLineEdit *>(editor);
            if (name) {
                model->setData(index, name->text());
            }
        }
        else if (index.column() == 1) {
            QLineEdit *numberEditor = qobject_cast<QLineEdit *>(editor);
            if (numberEditor) {
                model->setData(index, numberEditor->text());
            }
        }else if(index.column() == 2){
            QSpinBox *expMonth = static_cast<QSpinBox *>(editor);
            if(expMonth){
                model->setData(index, expMonth->value());
            }
        }else if (index.column() == 3) {
            QSpinBox *expYear = static_cast<QSpinBox *>(editor);
            if(expYear){
                model->setData(index, expYear->value());
            }
        }else if (index.column() == 4){
            QLineEdit *cvv = qobject_cast<QLineEdit *>(editor);
            if (cvv) {
                model->setData(index, cvv->text());
            }
        }
}

void CreditCardDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    editor->setGeometry(option.rect);
}

void CreditCardDelegate::SaveItem()
{
    const QModelIndex &index=(const QModelIndex)sender()->property("index").toModelIndex();
    qDebug()<<"Save"<<index.row() <<" row";
    QStringList list=index.model()->data(index, GetListAtRowRole).toStringList();
    qDebug()<<"Saving content: "<<list;

    QString oldName=index.model()->data(index, GetFileNameRole).toString();
    QString fileName;
    if(oldName.isEmpty())
        fileName = QFileDialog::getSaveFileName(new QWidget, tr("Select file name"), "");
    else
        fileName = QFileDialog::getSaveFileName(new QWidget, tr("Select file name"), oldName);

    if (fileName != "") {
        QAbstractItemModel *model=(QAbstractItemModel *)index.model();
        model->setData(index, fileName, SaveToFileRole);
        model->setData(index, fileName, SetFilenameRole);
    }
}

void CreditCardDelegate::LoadItemFromFile()
{
    const QModelIndex &index=sender()->property("index").toModelIndex();
    qDebug()<<"Load"<<index.row() <<" row";

    QString oldName=index.model()->data(index, GetFileNameRole).toString();
    QString fileName;
    if(oldName.isEmpty())
    {
        fileName = settings->value(CCFileNameKey).toString();
        fileName = QFileDialog::getOpenFileName(new QWidget, tr("Open user profile file"),fileName);
    }
    else
        fileName = QFileDialog::getOpenFileName(new QWidget, tr("Open user profile file"), oldName);

    if (fileName != "") {
        QAbstractItemModel *model=(QAbstractItemModel *)index.model();
        model->setData(index, fileName, LoadFromRole);
        model->setData(index, fileName, SetFilenameRole);
    }
}
